﻿<cfscript>
/** 
* @hint "acadmicWorkLoad - WorkLoad"
*/ 

component 
	extends="MachII.framework.Listener" 
	displayname="acadmicManager.listeners.humanResource.workloadListener" 
	output=false 
	depends="workloadFactory, sequenceWorkLoad, sequenceHRWorkload"
	{
	
	/**
	* @hint "Configures this listener as part of the Mach-II framework"
	*/
	public void function configure() output=false {} 
	
	
	/**
	* @hint "删除工作量计算公式"
	* 
	* @event "MachII Event Object"
	*/
	public void function deleteWorkLoadFormula( required MachII.framework.Event event ) output=false {
		
		var functionId = event.getArg("FunID");
		var sql = "";
		var queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
		
			sql = "DELETE
					FROM t_workload_fun a 
					WHERE a.fun_id = :functionId ";
			
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="functionId", value=functionId, cfsqltype="cf_sql_char" );
			
			queryObj.execute( sql=sql );
			
		}
		
	}

	
	/**
	* @hint "更新工作量计算公式"
	* 
	* @event "MachII Event Object"
	*/
	public void function updateWorkLoadFormula( required MachII.framework.Event event ) output=false {
		var sql = "";
		var queryObj = "";
		
		var templateId = event.getArg("FunMethod");
		var functionId = event.getArg("FunID");
		
		var argsArray = getworkloadFactory().getLatexTemplateArgs( templateId );
		
		var argString = "";
		
		for ( var argSet in argsArray ) {
			
			if ( argSet[3] eq "F" and event.isArgDefined( argSet[1] ) ) {
				
				argString = listAppend( argString, argSet[1] & "=" & event.getArg(argSet[1]), "," );
				
			}
			
		}
		
		if ( argString eq "" ) {
			argString = "NOP";
		}
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			var FunName = event.getArg("FunName");
			var FunDescription = event.getArg("FunDescription");

			var FunMethod = templateId;
			var FunArgs = argString;
			
			sql = "UPDATE t_workload_fun a 
					SET a.fun_name = :FunName ,
						a.fun_describe = :FunDescription ,
						a.fun_meta = :FunMethod ,
						a.fun_args = :FunArgs 
					WHERE a.fun_id = :functionId ";
					
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="FunName", value=FunName, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="FunDescription", value=FunDescription, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="FunMethod", value=FunMethod, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="FunArgs", value=FunArgs, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="functionId", value=functionId, cfsqltype="cf_sql_char" );
			
			queryObj.execute( sql=sql );
								
		}
		
	}
	
	
	/**
	* @hint "新建工作量计算公式"
	* 
	* @event "MachII Event Object"
	*/
	public void function createWorkLoadFormula( required MachII.framework.Event event ) output=false {
		var sql = "";
		var queryObj = "";
		
		var templateId = event.getArg("FunMethod");
		
		var argsArray = getworkloadFactory().getLatexTemplateArgs( templateId );
		
		var argString = "";
		
		for ( var argSet in argsArray ) {
			
			if ( argSet[3] eq "F" and event.isArgDefined( argSet[1] ) ) {
				
				argString = listAppend( argString, argSet[1] & "=" & event.getArg(argSet[1]), "," );
				
			}
			
		}
		
		if ( argString eq "" ) {
			argString = "NOP";
		}
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			var sequenceWorkLoad = getsequenceWorkLoad();
			
			sequenceWorkLoad.setFunMethod(templateId);
			
			var functionId = sequenceWorkLoad.getID();
			var FunName = event.getArg("FunName");
			var FunDescription = event.getArg("FunDescription");
			var FunMethod = templateId;
			var FunArgs = argString;
			
			sql = "INSERT INTO t_workload_fun ( 
							fun_name, 
							fun_describe,
							fun_meta, 
							fun_args, 
							fun_id )
					VALUES(
						 :FunName ,
						 :FunDescription ,
						 :FunMethod ,
						 :FunArgs ,
						 :functionId
					) ";
			
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="FunName", value=FunName, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="FunDescription", value=FunDescription, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="FunMethod", value=FunMethod, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="FunArgs", value=FunArgs, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="functionId", value=functionId, cfsqltype="cf_sql_char" );
			
			queryObj.execute( sql=sql );
			
		}
		

	}
	
	
	/**
	* @hint "更新编制分类"
	* 
	* @event "MachII Event Object"
	*/
	public void function updateHRWorkLoad( required MachII.framework.Event event ) output=false {
		
		var typeId = event.getArg("HRType");
		var sql = "";
		var queryObj = "";

		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			var TypeName = event.getArg("TypeName") ;
			var Period = event.getArg("Period");
			var TypeNotice = event.getArg("TypeNotice") ;
			
			sql = "UPDATE t_hr_workload a 
					SET a.type_name = :TypeName ,
						a.type_desc = :TypeNotice ,
						a.type_period = :Period 
					WHERE a.hrt = :typeId "; 
					
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="TypeName", value=TypeName, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="TypeNotice", value=TypeNotice, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="Period", value=Period, cfsqltype="cf_sql_integer" );
			queryObj.addParam( name="typeId", value=typeId, cfsqltype="cf_sql_char" );
			
			queryObj.execute( sql=sql );
		
		}
	}
	
	
	/**
	* @hint "创建编制分类"
	* 
	* @event "MachII Event Object"
	*/
	public void function createHRWorkLoad( required MachII.framework.Event event ) output=false {
		var sql = "";
		var queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			var sequenceHRWorkload = getsequenceHRWorkload();
			var typeId = sequenceHRWorkload.getID();
			
			var TypeName = event.getArg("TypeName"); 
			var Period = event.getArg("Period"); 
			var TypeNotice = event.getArg("TypeNotice");
			
			sql = "INSERT INTO t_hr_workload (
						   hrt,
						   type_name,
						   type_desc,
						   type_period
					) VALUES (
							:typeId, 
							:TypeName,
							:TypeNotice,
							:Period 
					)";
					
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="typeId", value=typeId, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="TypeName", value=TypeName, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="TypeNotice", value=TypeNotice, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="Period", value=Period, cfsqltype="cf_sql_integer" );
			
			queryObj.execute( sql=sql );
		}
	}
	
	/**
	* @hint "删除编制分类"
	* 
	* @event "MachII Event Object"
	*/
	public void function removeHRWorkLoad( required MachII.framework.Event event ) output=false {
		
		var typeId = event.getArg("HRType");
		var sql = "";
		var queryObj = "";
		

		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "DELETE 
					FROM t_hr_workload a 
					WHERE a.hrt = :typeId ";
					
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="typeId", value=typeId, cfsqltype="cf_sql_char" );
			
			queryObj.execute( sql=sql );
								
		}
		
	}
	
	/**
	* @hint "保存课程系数设置"
	* 
	* @event "MachII Event Object"
	*/
	public void function saveCourseWorkLoad( required MachII.framework.Event event ) output=false {
		var sql = "";
		var queryObj = "";
		var CourseID = event.getArg("CourseID");
		var Keywords = event.getArg("Keywords");

		var CourseName = event.getArg("CourseName");
		var HRWorkLoadID = event.getArg("HRWorkLoad");
		var TWorkLoadID = event.getArg("TWorkLoad");
		var PWorkLoadID = event.getArg("PWorkLoad");
		var LanguageWeight = event.getArg("LanguageWeight");
		var PracticeWeight = event.getArg("PracticeWeight");
		var FirstWeight = event.getArg("FirstWeight");
		var SecondWeight = event.getArg("SecondWeight");
		var ThirdWeight = event.getArg("ThirdWeight");
		var TeacherWorkDivide = FirstWeight & ":" & SecondWeight & ":" & ThirdWeight;
		
		sql = "SELECT c.cid,
					   c.theory_fun,
					   c.practice_fun,
					   c.lang_weight,
					   c.prac_weight,
					   c.hrt,
					   c.work_divide 
				FROM t_course_workload c 
				WHERE c.cid = :CourseID ";
				
		queryObj = new Query( datasource=application.dnsMaster );
		queryObj.addParam( name="CourseID", value=CourseID, cfsqltype="cf_sql_char" );
		
		rs_CourseWorkLoads = queryObj.execute( sql=sql ).getResult();
	
		Transaction{
			
			if(rs_CourseWorkLoads.recordCount EQ 0){
				sql = "INSERT INTO t_course_workload c(
							   c.cid,
							   c.theory_fun,
							   c.practice_fun,
							   c.lang_weight,
							   c.prac_weight,
							   c.hrt,
							   c.work_divide
						)
						VALUES(
							   :CourseID,
							   :TWorkLoadID,
							   :PWorkLoadID,
							   :LanguageWeight,
							   :PracticeWeight,
							   :HRWorkLoadID,
							   :TeacherWorkDivide
						)";
						
				queryObj = new Query( datasource=application.dnsMaster );
				queryObj.addParam( name="CourseID", value=CourseID, cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="TWorkLoadID", value=TWorkLoadID, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="PWorkLoadID", value=PWorkLoadID, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="LanguageWeight", value=LanguageWeight,cfsqltype="cf_sql_decimal",scale = 3 );
				queryObj.addParam( name="PracticeWeight", value=PracticeWeight, cfsqltype="cf_sql_decimal",scale =3 );
				queryObj.addParam( name="HRWorkLoadID", value=HRWorkLoadID, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="TeacherWorkDivide", value=TeacherWorkDivide, cfsqltype="cf_sql_varchar" );
				
				queryObj.execute( sql=sql );
			
			}else{
				sql = "UPDATE  t_course_workload c
						SET
							   c.theory_fun = :TWorkLoadID,
							   c.practice_fun = :PWorkLoadID,
							   c.lang_weight = :LanguageWeight,
							   c.prac_weight = :PracticeWeight,
							   c.hrt = :HRWorkLoadID,
							   c.work_divide = :TeacherWorkDivide 
						WHERE c.cid = :CourseID 
						";
						
				queryObj = new Query( datasource=application.dnsMaster );
				queryObj.addParam( name="CourseID", value=CourseID, cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="TWorkLoadID", value=TWorkLoadID, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="PWorkLoadID", value=PWorkLoadID, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="LanguageWeight", value=LanguageWeight,cfsqltype="cf_sql_decimal",scale = 3 );
				queryObj.addParam( name="PracticeWeight", value=PracticeWeight, cfsqltype="cf_sql_decimal",scale =3 );
				queryObj.addParam( name="HRWorkLoadID", value=HRWorkLoadID, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="TeacherWorkDivide", value=TeacherWorkDivide, cfsqltype="cf_sql_varchar" );
				
				queryObj.execute( sql=sql );
			}
		}
		
		event.setArg("CID",CourseID);
		event.setArg("Keywords",Keywords);
		
	}
	
}

</cfscript>